<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>scratch card</title>
  <style>
    #canvas {
      position: absolute;
      top: 0;
      left: 0;
      z-index: 10;
    }

    .text {
      position: absolute;
      top: 0;
      left: 0;
      font-size: 30px;
      width: 300px;
      height: 150px;
      text-align: center;
      line-height: 150px;
      background-color: red;
      color: #fff;
    }
  </style>
</head>

<body>
  <canvas id="canvas"></canvas>
  <div class="text"></div>
  <script>
    const text = document.querySelector('.text');
    const lotteryList = ['特等奖', '一等奖', '二等奖', '三等奖', '谢谢参与']
    const lotteryName = lotteryList[Math.floor(Math.random() * lotteryList.length)];
    text.innerText = lotteryName;
    const canvas = document.getElementById('canvas');
    const ctx = canvas.getContext('2d');
    let canDraw = false;
    canvas.width = 300;
    canvas.height = 150;
    ctx.fillStyle = 'gray';
    ctx.fillRect(0, 0, 300, 150);
    canvas.addEventListener('mousedown', () => {
      canDraw = true;
      canvas.style.cursor = 'pointer';
    })
    canvas.addEventListener('mouseup', () => {
      canDraw = false;
      canvas.style.cursor = 'default';
    })
    canvas.addEventListener('mousemove', (e) => {
      if (canDraw) {
        const x = e.clientX - canvas.offsetLeft;
        const y = e.clientY - canvas.offsetTop;
        goDraw(x, y);
      }
    })
    const goDraw = (x, y) => {
      // 设置要在绘制新形状时应用的合成操作的类型
      // 这里设置为'destination-out'，仅保留现有画布内容和新形状不重叠的部分
      ctx.globalCompositeOperation = 'destination-out'
      ctx.beginPath();
      ctx.arc(x, y, 20, 0, 2 * Math.PI);
      ctx.fill();
    }
  </script>
</body>

</html>